//
//  LYTabBarController.m
//  LYDevelopKit
//
//  Created by 李阳 on 2018/5/25.
//

#import "LYTabBarController.h"
#import "LYNavigationController.h"

@interface LYTabBarItem ()

@end
@implementation LYTabBarItem

- (instancetype)init
{
    self = [super init];
    if (!self) return nil;
    self.navClsEmbedIn = [LYNavigationController class];
    return self;
}

- (void)setNavClsEmbedIn:(Class)cls
{
    if (cls == nil)
    {
        _navClsEmbedIn = nil;
        return;
    }
    NSParameterAssert([cls isKindOfClass:[UINavigationController class]]);
    _navClsEmbedIn = cls;
}

- (void)setVcCls:(Class)vcCls
{
    NSParameterAssert(vcCls || [vcCls isKindOfClass:[UIViewController class]]);
    _vcCls = vcCls;
}

@end

@interface LYTabBarController ()
<UITabBarControllerDelegate>
/// 上次选中的索引
@property (nonatomic, assign)NSInteger lastSelIdx;

@end

@implementation LYTabBarController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.lastSelIdx = -1;
    self.delegate = self;
}

- (void)setItemsEmbedIn:(NSArray<LYTabBarItem *> *)items
{
    _itemsEmbedIn = items;
    [items enumerateObjectsUsingBlock:^(LYTabBarItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self setupOneViewControllerWith:obj];
    }];
}

- (__kindof UIViewController *)setupOneViewControllerWith:(LYTabBarItem *)item;
{
    UIViewController *vc = [[item.vcCls alloc] init];
    vc.tabBarItem.title = item.title;
    vc.tabBarItem.image = item.image;
    vc.tabBarItem.selectedImage = item.selectedImage;
    if (item.navClsEmbedIn)
    {
        vc = [[item.navClsEmbedIn alloc]initWithRootViewController:vc];
    }
    [self addChildViewController:vc];
    return vc;
}

#pragma mark - UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    UIViewController *(^findOp)(void) = ^{
        if ([viewController isKindOfClass:[UINavigationController class]])
        {
            UINavigationController *nav = (UINavigationController *)viewController;
            return nav.topViewController;
        }
        else
        {
            return viewController;
        }
    };
    [findOp() tabBarItemDidSelect];
    if (_lastSelIdx == self.selectedIndex)
    {
        [findOp() tabBarItemDidSelectAgain];
    }
    self.lastSelIdx = self.selectedIndex;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end




@implementation UIViewController (LYTabBarController)

/// 被标签控制器再次被选中，子类可重写
- (void)tabBarItemDidSelectAgain {}

/// 被标签控制器被选中，子类可重写
- (void)tabBarItemDidSelect {}

@end
